package com.skcc.cop.mrtaxi.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

public class FileUtil {

	/**
	 * @param fileName
	 *            http://asdf/asdf.pdf
	 * @return pdf
	 */
	public static String findFileExtension(String fileName) {
		String split[] = fileName.split("\\.");
		return split[split.length - 1].replace(".", "");
	}

	/**
	 * @param url
	 *            http://www.asfd.com/filename.pdf
	 * @return filename.pdf
	 */
	public static String findFileName(String url) {
		String split[] = url.split("/");
		return split[split.length - 1].replace("/", "");
	}

	public static boolean makeDirectory(String absPath, String dirNameSlash) {
		File dir = new File(absPath + dirNameSlash);
		if (!dir.exists()) {
			return dir.mkdirs();
		}
		return true;
	}

	public static boolean deleteAndmakeFile(String dirAbsPath, String fileNameSlash) {
		boolean reBool;
		File file = new File(dirAbsPath + fileNameSlash);
		if (file.exists() && file.isFile() && file.canRead()) {
			if (file.delete()) {
				try {
					reBool = file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
					return false;
				}
				return reBool;
			}
		} else if (!file.exists()) {
			try {
				reBool = file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
			return reBool;
		}
		return false;
	}

	public static boolean makeFile(String dirAbsPath, String fileNameSlash) {
		boolean reBool;
		File file = new File(dirAbsPath + fileNameSlash);
		if (file.exists() && file.isFile() && file.canRead()) {
			return true;
		} else if (!file.exists()) {
			try {
				reBool = file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
			return reBool;
		}
		return false;
	}

	public static ArrayList<String> readContentList(String absFilePath) {
		File file = new File(absFilePath);
		ArrayList<String> lineInputList = new ArrayList<String>();
		lineInputList.removeAll(lineInputList);
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			String line = null;
			while ((line = br.readLine()) != null) {
				lineInputList.add(line);
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lineInputList;
	}

	public static String readContents(String absFilePath) {
		File file = new File(absFilePath);
		String lineString = "";
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
			String line = "";
			while ((line = br.readLine()) != null) {
				lineString = lineString + line + "\n";
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lineString;
	}

	public static boolean writeContents(String absFilePath, String contents) {
		File file = new File(absFilePath);
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
			bw.write(contents);
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean writeAppendContent(String absFilePath, String content) {
		boolean reBool = true;

		FileOutputStream fos = null;
		byte[] buffer = content.getBytes();
		File file = new File(absFilePath);
		if (file.exists() && file.isFile() && file.canRead()) {
			try {
				fos = new FileOutputStream(file, true);
				if (fos != null) {
					fos.write(buffer);
					fos.write("\n".getBytes());
				}
			} catch (Exception e) {
				reBool = false;
				e.printStackTrace();
			} finally {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
					reBool = false;
				}
			}
		}
		return reBool;
	}

	private static ArrayList<String> dirAndfileList = new ArrayList<String>();

	public static ArrayList<String> getFileListInDir(String dirAbsPath) {
		dirAndfileList.removeAll(dirAndfileList);
		recurse(new File(dirAbsPath), 0);
		return dirAndfileList;
	}

	private static void recurse(File dirFile, int depth) {
		String contents[] = dirFile.list();
		for (int i = 0; i < contents.length; i++) {
			File child = new File(dirFile, contents[i]);
			if (child.isDirectory()) {
				recurse(child, depth + 1);
			} else {
				dirAndfileList.add(dirFile + "\\" + contents[i]);
			}
		}
	}

}
